package com.itheima.web;

import com.itheima.pojo.Spec;
import com.itheima.service.GoodsSpecService;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/spec")
public class GoodsSpecController {
    @Autowired
    private GoodsSpecService goodsSpecService;

    @GetMapping
    public Result findAll() {
        return goodsSpecService.findAllGoodsSpec();
    }

    @GetMapping("/search/{currentPage}/{pageSize}")
    public Result findAllByPage(@PathVariable("currentPage") String current, @PathVariable("pageSize") String pageSize) {
        return goodsSpecService.findAllByPageGoodsSpec(Integer.parseInt(current), Integer.parseInt(pageSize));
    }

    @PostMapping()
    public Result addGoodsSpec(@RequestBody Spec spec) {
        return goodsSpecService.addGoodsSpec(spec);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return goodsSpecService.findByIdGoodsSpec(Long.parseLong(id));
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable("id")String  id,@RequestBody Spec spec) {
        return goodsSpecService.updateByIdGoodsSpec(Long.parseLong(id),spec);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Long  id) {
        return goodsSpecService.deleteByIdGoodsSpec(id);
    }
}
